import java.io.File
import scala.io.Source
import scala.collection.mutable.Map

object AI {
  def main(args: Array[String]): Unit = {
    val filePath = "C:\\Users\\PC-608\\Desktop\\word.txt"
    val wordFrequencyMap = countWordFrequencies(filePath)

    wordFrequencyMap.foreach { case (word, frequency) =>
      println(s"$word: $frequency")
    }
  }

  def countWordFrequencies(filePath: String): Map[String, Int] = {
    val source = Source.fromFile(new File(filePath))
    try {
      val text = source.mkString
      val words = text.split("[^a-zA-Z]+").filter(_.nonEmpty).map(_.toLowerCase)
      val wordFrequencyMap = Map[String, Int]()

      words.foreach { word =>
        wordFrequencyMap(word) = wordFrequencyMap.getOrElse(word, 0) + 1
      }

      wordFrequencyMap
    } finally {
      source.close()
    }
  }
}